package com.wxg.service.impl;

import com.wxg.common.core.dao.mysql.global.Dict;
import com.wxg.common.core.dto.Address;
import com.wxg.dao.mysql.mapper.DictMapper;
import com.wxg.service.TestService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author void
 * @date 2020/7/22 17:27
 * @desc
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TestServiceImpl implements TestService {

    private final DictMapper dictMapper;
    private final MongoTemplate mongoTemplate;

    @Transactional
    @Override
    public void test(Boolean flag) {
        Dict dict = new Dict();
        dict.setName("test");
        dict.setCode("test");
        dictMapper.insertSelective(dict);

        Address address = new Address();
        address.setPhone("1111111");
        mongoTemplate.insert(address);

        if(flag){
            throw new RuntimeException("手动引起异常");
        }
    }

    /**
     * 测试多个子方法进行db操作是否可以使用事务
     * @param flag
     */
    @Transactional
    @Override
    public void multipleMethod(Boolean flag) {
        method1();
        method2();
        if(flag){
            throw new RuntimeException("手动引起异常");
        }
    }

    public void method1(){
        Dict dict = new Dict();
        dict.setName("test1");
        dict.setCode("test1");
        dictMapper.insertSelective(dict);
    }

    public void method2(){
        Dict dict = new Dict();
        dict.setName("test2");
        dict.setCode("test2");
        dictMapper.insertSelective(dict);
    }
}
